/*    BlueTx
 *
 *    Copyright (c) 2007 Sun Ribo <Sean.Sun@bea.com>
 *
 *
 */

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/utsname.h>

#include <atmi.h>
#include <fml32.h>
#include <tpadm.h>


#include "struct.h"

extern void ulog( const char *fmt, ... );

#ifdef _USEMAIN
#define ulog  userlog
#endif

enum {
	HANDINIT = 91,
	HANDACK,
	GETSVRDATA,
	GETINITDATA,
	CLOSECONNECT
};

typedef struct _tuxsvr_t
{
    char rectime[32];
    char svrname[100];
    int queid;
    int pid;
    int rqdone;
    char currsvc[20];
    int svrmin;
    int svrmax;
    char svrmem[32];
    char flag[4];
} TUXSVR;

typedef struct _tuxque_t
{
    char rectime[32];
    char svrname[100];
    char quename[32];
    int svrcnt;
    int wkqueued;
    int queid;			/* OS IPC Queue ID */
    int queued;
    char avelen[4];
    char machine[12];
    char flag[4];
} TUXQUE;

typedef struct _tuxclt_t
{
    char rectime[32];
    char cltname[100];
    int cltpid;
    char cltaddr[32];
    char cltstatus[10];
    char cltcontime[20];
    char flag[4];
} TUXCLT;

typedef struct _tuxsys_t
{
    char rectime[32];
    char corefind[1024];
    char errfind[512];
    char warnfind[512];
    char largefile[512];
    char freemem[128];
    char idlecpu[32];
    char flag[4];
} TUXSYS;

typedef struct _tuxstat_t {
	int svrcnt;
	int svccnt;
	int quecnt;
	int cltcnt;
} TUXSTAT;

TUXSTAT tuxstat;

typedef struct _initbuf_t {
	char tuxversion[32];
	char tuxbootat[32];
	char osversion[32];
	char agentversion[32];
} INITBUF;

char ident[] =
    "@(#) src/agent.c       $Revision: 0.9 $ $Author: Sunribo <Sean.sun@bea.com> $";

time_t tm;
char SYSTEM_IS_STOP[32] = "N/A";

int system_flag = 0;

static char* _svrname_trim( char *svrname )
{
#define SPLITCHR '/'
	static char newsvrname[128];
	char *pLast = svrname+strlen(svrname);
	char *pFirst = svrname;
	int offset = 0;

	while( *pLast != SPLITCHR ) pLast --;

 	offset = pLast - pFirst + 1;

	strcpy( newsvrname, svrname + offset );

	return newsvrname;
}

static char *
_getTuxVersion ()
{
    const char template[128] = "tmadmin -v 2>.ver;cat .ver|grep Ver|awk '{print \"V\"$5 $6 \"Patch \" $9}'";
    char shellcode[128], linestr[512],*ptr = NULL;
    static char version[32];

    FILE *fp = NULL;

    strcpy (version, "N/A");

    strcpy (shellcode, template);

    fp = popen (shellcode, "r");
    if (fp == NULL)
        return version;
    fgets (version, sizeof (version), fp);
    fclose (fp);

    version[strlen (version) - 1] = '\0';

    return version;
}
static char *
_getOSInfo ()
{
    static char version[32] = "N/A";
    struct utsname host;
    uname( &host) ;
#ifdef _IS_LINUX
    sprintf( version, "%s - %s %s", host.nodename, host.sysname , host.release );
#elif _IS_AIX5
    sprintf( version, "%s - %s %s.%s", host.nodename, host.sysname , host.version, host.release );
#endif
    return version;
}

static char *
_getAgentVer()
{
	extern LICENSE lic;
	static char agentver[32];
	if( strcmp(lic.license, "TRIAL") == 0 )
		sprintf( agentver,"%s - TRIAL LICENSE",  lic.version );
	else 
		sprintf( agentver,"%s - FORMAL LICENSE",  lic.version );

	return agentver;
}


static char *
_getMemByID (int pid)
{
#ifdef _IS_LINUX
    const char template[128] =
    "ps avx|grep %d|grep -v grep|awk '{print $8\"K\"}'";
#elif _IS_AIX5
    const char template[128] =
    "ps avx|grep %d|grep -v grep|awk '{print $7\"K\"}'";
#endif
    char shellcode[128];
    static char memuse[32];
    FILE *fp = NULL;

    strcpy (memuse, "N/A");

    sprintf (shellcode, template, pid);
    fp = popen (shellcode, "r");
    if (fp == NULL)
        return memuse;
    fgets (memuse, sizeof (memuse), fp);
    fclose (fp);
    memuse[strlen (memuse) - 1] = '\0';

    return memuse;
}

static char* _getFreeMem()
{
#ifdef _IS_LINUX
    const char template[128] = "free memory|grep Mem|awk '{printf \"%4.2fM\", $4 / 1024}'";
#elif _IS_AIX5
    const char template[128] = "vmstat |grep -v -E \"cpu|id|-\"|awk '{printf \"%4.2fM\", $4 * 4 / 1024}'";
#endif
    char shellcode[128];
    static char freemem[32];
    FILE *fp = NULL;

    strcpy (freemem, "N/A");

    strcpy( shellcode, template );
    fp = popen (shellcode, "r");
    if (fp == NULL)
        return freemem;
    fgets (freemem, sizeof (freemem), fp);
    fclose (fp);
    /*
    freemem[strlen (freemem) - 1] = '\0';
    */

    return freemem;
}

static char* _getIdleCPU()
{
#ifdef _IS_LINUX
    const char template[128] = "vmstat 1 1|grep -v cpu|grep -v id|awk '{print $15}'";
#elif _IS_AIX5
    const char template[128] = "vmstat 1 1|grep -v cpu|grep -v id|awk '{print $16}'|tail -1";
#endif
    char shellcode[128];
    static char idlecpu[32];
    FILE *fp = NULL;

    strcpy (idlecpu, "N/A");

    strcpy( shellcode, template );
    fp = popen (shellcode, "r");
    if (fp == NULL)
        return idlecpu;
    fgets (idlecpu, sizeof(idlecpu), fp);
    fclose (fp);

    idlecpu[strlen(idlecpu) - 1] = '\0';

    return idlecpu;
}

static char* _getTuxInfo ()
{
    FBFR32 *buff_in, *buff_out;
    long id;
    long olen = 0, flags = 0;
    static char stime[100], bbltime[32]; 
    time_t time;
    struct tm *rectm = NULL;

    buff_in = (FBFR32 *) tpalloc ("FML32", NULL, 0);
    buff_out = (FBFR32 *) tpalloc ("FML32", NULL, 0);
    Fchg32 (buff_in, TA_OPERATION, 0, "GET", 0);
    Fchg32 (buff_in, TA_CLASS, 0, "T_SERVER", 0);
	Fchg32 (buff_in, TA_SERVERNAME, 0, "BBL", 0);

    flags = MIB_LOCAL;
    Fchg32 (buff_in, TA_FLAGS, 0, (char *) &flags, 0);

    olen = Fsizeof32 (buff_out);

    if (tpcall
            (".TMIB", (char *) buff_in, 0, (char **) &buff_out, &olen,
             TPNOTRAN) < 0)
    {
        ulog ("txmibclt failed, %s\n", tpstrerror (tperrno));
		system_flag = 0;
        return SYSTEM_IS_STOP;
    }

    Fgets32 (buff_out, TA_TIMESTART, 0, stime);	/* When this Server booted */
    time = atol (stime);

    rectm = localtime (&time);
    sprintf (bbltime, "%d-%02d-%02d %02d:%02d:%02d",
                 rectm->tm_year + 1900, rectm->tm_mon + 1, rectm->tm_mday,
                 rectm->tm_hour, rectm->tm_min, rectm->tm_sec);

    tpfree ((char *) buff_in);
    tpfree ((char *) buff_out);

    return bbltime;
}

char *
getSrvInfo ()
{
    FBFR32 *buff_in, *buff_out;
    long id;
    long olen = 0, flags = 0;
    char stime[100], svrnum[4], svrname[128], svrdone[10], svrpid[10],
    cursvc[32], svrtime[32], bbltime[32];
    char onetuxsvr[4096];
    char svrmin[4], svrmax[4], queid[16];
    time_t time;
    struct tm *rectm = NULL;
    char *srvinfo = NULL;

    TUXSVR tuxsvr;

    memset ((void *) &tuxsvr, 0, sizeof (TUXSVR));

    buff_in = (FBFR32 *) tpalloc ("FML32", NULL, 0);
    buff_out = (FBFR32 *) tpalloc ("FML32", NULL, 0);
    Fchg32 (buff_in, TA_OPERATION, 0, "GET", 0);
    Fchg32 (buff_in, TA_CLASS, 0, "T_SERVER", 0);

    flags = MIB_LOCAL;
    Fchg32 (buff_in, TA_FLAGS, 0, (char *) &flags, 0);

    olen = Fsizeof32 (buff_out);

    if (tpcall
            (".TMIB", (char *) buff_in, 0, (char **) &buff_out, &olen,
             TPNOTRAN) < 0)
    {
        ulog ("txmibclt failed, %s\n", tpstrerror (tperrno));
		system_flag = 0;
        return SYSTEM_IS_STOP;
    }

    Fgets32 (buff_out, TA_OCCURS, 0, svrnum);

	tuxstat.svrcnt = atoi(svrnum);

    srvinfo = (char *) malloc (atoi (svrnum) * sizeof (TUXSVR) + 1024);
    memset (srvinfo, 0, strlen (srvinfo));

    /*
      strcpy (srvinfo,
    	  "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<SERVERINFO>\n");
    */

    for (id = 0; id < atoi (svrnum); id++)
    {
        Fgets32 (buff_out, TA_SERVERNAME, id, svrname);	/* Name of this Server */

        Fgets32 (buff_out, TA_RQID, id, queid);	/* The ID of the specify queue */

        Fgets32 (buff_out, TA_TOTREQC, id, svrdone);	/* Total Request Done By this Server */

        Fgets32 (buff_out, TA_PID, id, svrpid);	/* Process ID  */

        Fgets32 (buff_out, TA_MIN, id, svrmin);	/* MIN Number of this server  */

        Fgets32 (buff_out, TA_MAX, id, svrmax);	/* MAX Number of this server  */

        Fgets32 (buff_out, TA_CURRSERVICE, id, cursvc);	/* Current Service Working On */
        if (cursvc[0] == '\0')
            strcpy (cursvc, "IDLE");

#ifdef _DEBUG
        printf
        ("Svr: [%32s]  QueID: [%8s]  PID: [%7s]  Done: [%8s]  CurSvc: [%10s]  MIN: [%2s] MAX: [%2s] Booted: [%s]\n",
         svrname, queid, svrpid, svrdone, cursvc, svrmin, svrmax, svrtime);
#endif
        strcpy (tuxsvr.svrname, _svrname_trim(svrname));
        rectm = localtime (&tm);
        sprintf (tuxsvr.rectime, "%d-%02d-%02d %02d:%02d:%02d",
                 rectm->tm_year + 1900, rectm->tm_mon + 1, rectm->tm_mday,
                 rectm->tm_hour, rectm->tm_min, rectm->tm_sec);
        tuxsvr.queid = atol (queid);
        tuxsvr.pid = atol (svrpid);
        tuxsvr.rqdone = atol (svrdone);
        strcpy (tuxsvr.currsvc, cursvc);
        tuxsvr.svrmin = atoi (svrmin);
        tuxsvr.svrmax = atoi (svrmax);
        strcpy (tuxsvr.svrmem, _getMemByID (tuxsvr.pid));

        strcpy (tuxsvr.flag, "");

        sprintf (onetuxsvr,
                 "<SERVER Server=\"%s\" Queueid=\"%d\" ProcessID=\"%d\" RqDone=\"%d\" CurrSvc=\"%s\"\
	SvrMin=\"%d\" SvrMax=\"%d\" UseMem=\"%s\"></SERVER>\n",
                 tuxsvr.svrname, tuxsvr.queid, tuxsvr.pid, tuxsvr.rqdone, tuxsvr.currsvc,
                 tuxsvr.svrmin, tuxsvr.svrmax, tuxsvr.svrmem);

        strcat (srvinfo, onetuxsvr);
    }
    tpfree ((char *) buff_in);
    tpfree ((char *) buff_out);

    return srvinfo;
}

char *
getQueInfo ()
{
    FBFR32 *buff_in, *buff_out;
    long id;
    long olen =0, flags = 0;
    char tmpstr[1024];
    char onetuxque[4096];
    char *queinfo = NULL;
    char svrname[128], quenum[4], queued[4], svrcnt[4], queid[32], avelen[6],
    machiname[32];
    char wkqueued[12];
    struct tm *rectm = NULL;
    TUXQUE tuxque;


    memset ((void *) &tuxque, 0, sizeof (TUXQUE));
    memset (tmpstr, 0, sizeof (tmpstr));

    buff_in = (FBFR32 *) tpalloc ("FML32", NULL, 0);
    buff_out = (FBFR32 *) tpalloc ("FML32", NULL, 0);
    Fchg32 (buff_in, TA_OPERATION, 0, "GET", 0);
    Fchg32 (buff_in, TA_CLASS, 0, "T_QUEUE", 0);

    flags = MIB_LOCAL;
    Fchg32 (buff_in, TA_FLAGS, 0, (char *) &flags, 0);

    olen = Fsizeof32 (buff_out);

    if (tpcall
            (".TMIB", (char *) buff_in, 0, (char **) &buff_out, &olen,
             TPNOTRAN) < 0)
    {
        ulog ("txmibclt failed, %s\n", tpstrerror (tperrno));
		system_flag = 0;
        return SYSTEM_IS_STOP;
    }

    Fgets32 (buff_out, TA_OCCURS, 0, quenum);
	
	tuxstat.quecnt = atoi(quenum);

    queinfo = (char *) malloc (atoi (quenum) * sizeof (TUXQUE) + 1024);
    memset (queinfo, 0, strlen (queinfo));


    for (id = 0; id < atoi (quenum); id++)
    {
        Fgets32 (buff_out, TA_SERVERNAME, id, svrname);	/* Name of this Server */

        Fgets32 (buff_out, TA_MSG_QNUM, id, queued);	/* Total Enqueued Msg on this queue */

        Fgets32 (buff_out, TA_SERVERCNT, id, svrcnt);	/* Total count of servers on this queue */

        Fgets32 (buff_out, TA_RQID, id, queid);	/* The name of the specify queue */

        Fgets32 (buff_out, TA_WKQUEUED, id, wkqueued);	/* Wk Queued Info */

#ifdef _DEBUG
        printf ("Svr: [%32s]  QueID: [%8s]  SvrCnt: [%3s]  Queued: [%3s]\n",
                svrname, queid, svrcnt, queued);
#endif
        strcpy (tuxque.svrname, _svrname_trim(svrname));
        tuxque.queid = atol (queid);
        tuxque.svrcnt = atol (svrcnt);
        tuxque.queued = atol (queued);
        strcpy (tuxque.flag, "");
        sprintf (onetuxque,
                 "<QUEUE Server=\"%s\" Queueid=\"%d\" SrvCnt=\"%d\" Queued=\"%d\"></QUEUE>\n",
                 tuxque.svrname, tuxque.queid, tuxque.svrcnt, tuxque.queued);
        strcat (queinfo, onetuxque);
    }

    tpfree ((char *) buff_in);
    tpfree ((char *) buff_out);

    return queinfo;
}

char *
getCltInfo ()
{
    FBFR32 *buff_in, *buff_out;
    long id = 0;
    long olen, flags = 0;
    char cltname[128], cltnum[4], cltpid[8], cltaddr[128], cltstat[32];
    char onetuxclt[4096];
    char *cltinfo = NULL;
    char cltstart[100], cltcurr[100], elaptime[100];
    double time;
    struct tm *rectm = NULL;
    TUXCLT tuxclt;

    memset ((void *) &tuxclt, 0, sizeof (TUXCLT));

    buff_in = (FBFR32 *) tpalloc ("FML32", NULL, 0);
    buff_out = (FBFR32 *) tpalloc ("FML32", NULL, 0);
    Fchg32 (buff_in, TA_OPERATION, 0, "GET", 0);
    Fchg32 (buff_in, TA_CLASS, 0, "T_CLIENT", 0);

    flags = MIB_LOCAL;
    Fchg32 (buff_in, TA_FLAGS, 0, (char *) &flags, 0);

    olen = Fsizeof32 (buff_out);

    if (tpcall
            (".TMIB", (char *) buff_in, 0, (char **) &buff_out, &olen,
             TPNOTRAN) < 0)
    {
        ulog ("txmibclt failed, %s\n", tpstrerror (tperrno));
		system_flag = 0;
        return SYSTEM_IS_STOP;
    }
    Fgets32 (buff_out, TA_OCCURS, 0, cltnum);

	tuxstat.cltcnt = atoi(cltnum);

    cltinfo = (char *) malloc (atoi (cltnum) * sizeof (TUXCLT) + 1024);
    memset (cltinfo, 0, strlen (cltinfo));
    /*
      strcpy (cltinfo, "<CLIENT>");
    */

    for (id = 0; id < atoi (cltnum); id++)
    {
        Fgets32 (buff_out, TA_CLTNAME, id, cltname);	/* Name of this Client */

        Fgets32 (buff_out, TA_PID, id, cltpid);	/* The Process ID of this Client */

        Fgets32 (buff_out, TA_NADDR, id, cltaddr);	/* Th Address of this Client */
        if (cltaddr[0] == '\0')
            strcpy (cltaddr, "NATIVE CLIENT");

        Fgets32 (buff_out, TA_CURREQ, id, cltstat);	/* Th State of this Client */
        if (cltstat[0] == '0')
            strcpy (cltstat, "IDLE");
        else
            strcpy (cltstat, "BUSY");

        Fgets32 (buff_out, TA_TIMESTART, id, cltstart);	/* Th Address of this Client */
        Fgets32 (buff_out, TA_CURTIME, id, cltcurr);	/* Th Address of this Client */

        time = difftime ((time_t) atol (cltcurr), (time_t) atol (cltstart));
        if (time < 0)
            time = 0;
#ifdef _DEBUG
        printf
        ("Clt: [%16s]  CltPID: [%7s]  CltAddr: [%16s]  CltStat: [%s] Elapsed: [%5.0fs]\n",
         cltname, cltpid, cltaddr, cltstat, time);
#endif
        rectm = localtime (&tm);
        sprintf (tuxclt.rectime, "%d-%02d-%02d %02d:%02d:%02d",
                 rectm->tm_year + 1900, rectm->tm_mon + 1, rectm->tm_mday,
                 rectm->tm_hour, rectm->tm_min, rectm->tm_sec);
        strcpy (tuxclt.cltname, cltname);
        strcpy (tuxclt.cltaddr, cltaddr);
        strcpy (tuxclt.cltstatus, cltstat);
        tuxclt.cltpid = atol (cltpid);
        sprintf (tuxclt.cltcontime, "%5.0fs", time);

        sprintf (onetuxclt,
                 "<CLIENT Name=\"%s\" ClientPID=\"%d\" ClientAddr=\"%s\" Status=\"%s\" ConTime=\"%s\"></CLIENT>\n",
                 tuxclt.cltname, tuxclt.cltpid, tuxclt.cltaddr,
                 tuxclt.cltstatus, tuxclt.cltcontime, tuxclt.flag);
        /*
              sprintf (onetuxclt, "%s|%s|%d|%s|%s|%s|%s|\n", tuxclt.rectime,
        	       tuxclt.cltname, tuxclt.cltpid, tuxclt.cltaddr,
        	       tuxclt.cltstatus, tuxclt.cltcontime, tuxclt.flag);
        */
        strcat (cltinfo, onetuxclt);

    }
    /*
      printf("================\nTuxedo App: Total Client Number: [%s].\n\n", cltnum );
      */

    tpfree ((char *) buff_in);
    tpfree ((char *) buff_out);

    return cltinfo;
}

char *
getSysInfo ()
{
    long olen, flags = 0;
    static char appdir[512], ulogpfx[512];
    struct tm *rectm = NULL;
    struct stat stbuf;
    char corename[512], *onetuxsys = NULL;
    int ret = 0;
    FBFR32 *buff_in, *buff_out;
    TUXSYS tuxsys;

    memset ((void *) &tuxsys, 0, sizeof (TUXSYS));
    buff_in = (FBFR32 *) tpalloc ("FML32", NULL, 0);
    buff_out = (FBFR32 *) tpalloc ("FML32", NULL, 0);
    Fchg32 (buff_in, TA_OPERATION, 0, "GET", 0);
    Fchg32 (buff_in, TA_CLASS, 0, "T_MACHINE", 0);

    flags = MIB_LOCAL;
    Fchg32 (buff_in, TA_FLAGS, 0, (char *) &flags, 0);

    olen = Fsizeof32 (buff_out);

    if (tpcall
            (".TMIB", (char *) buff_in, 0, (char **) &buff_out, &olen,
             TPNOTRAN) < 0)
    {
        ulog ("txmibclt failed, %s\n", tpstrerror (tperrno));
		system_flag = 0;
        return SYSTEM_IS_STOP;
    }
    /*
       Fprint32( buff_out );
     */
    Fgets32 (buff_out, TA_APPDIR, 0, appdir);	/* Directory Name of Application */

    Fgets32 (buff_out, TA_ULOGPFX, 0, ulogpfx);	/* Prefix of the ULOG */
    if (ulogpfx[0] == '\0')
        strcpy (ulogpfx, "ULOG");

    rectm = localtime (&tm);
    sprintf (tuxsys.rectime, "%d-%02d-%02d %02d:%02d:%02d",
             rectm->tm_year + 1900, rectm->tm_mon + 1, rectm->tm_mday,
             rectm->tm_hour, rectm->tm_min, rectm->tm_sec);
    sprintf (corename, "%s/core", appdir);
    ret = stat (corename, &stbuf);
    if (ret == 0)
    {
        rectm = localtime (&stbuf.st_mtime);
        sprintf (tuxsys.corefind,
                 "Core File Find!!! Created at %d-%02d-%02d %02d:%02d:%02d",
                 rectm->tm_year + 1900, rectm->tm_mon + 1, rectm->tm_mday,
                 rectm->tm_hour, rectm->tm_min, rectm->tm_sec);
    }
    else
    {
        if (errno == ENOENT)
            strcpy (tuxsys.corefind, "");
    }
    tuxsys.corefind[strlen (tuxsys.corefind) - 1] = '\0';
    strcpy( tuxsys.freemem, _getFreeMem() );
    strcpy( tuxsys.idlecpu, _getIdleCPU() );

    /*
      fprintf( stderr, "%s|%s|%s|%s|%s|%s|\n", tuxsys.rectime, tuxsys.corefind, tuxsys.errfind,
    	tuxsys.warnfind, tuxsys.largefile, tuxsys.flag );
    */
    onetuxsys = (char *) malloc (sizeof (TUXSYS) + 1024);
    memset (onetuxsys, 0, sizeof (TUXSYS) + 12);
    /*
      sprintf (onetuxsys, "<TUXSYS>%s|%s|%s|%s|%s|%s|\n", tuxsys.rectime,
    	   tuxsys.corefind, tuxsys.errfind, tuxsys.warnfind, tuxsys.largefile,
    	   tuxsys.flag);
     */
    sprintf (onetuxsys,
             "<SYSTEM CoreFind=\"%s\" ErrorFind=\"%s\" WarnFind=\"%s\" LargeFile=\"%s\" \
			 FreeMem=\"%s\" IdleCPU=\"%s\" SvrCnt=\"%d\" QueCnt=\"%d\" CltCnt=\"%d\"></SYSTEM>\n",
             tuxsys.corefind, tuxsys.errfind, tuxsys.warnfind, tuxsys.largefile,
			 tuxsys.freemem, tuxsys.idlecpu, tuxstat.svrcnt, tuxstat.quecnt, tuxstat.cltcnt );

    tpfree ((char *) buff_in);
    tpfree ((char *) buff_out);

    return onetuxsys;
}

char *
getInitInfo ()
{
    INITBUF initbuf;
	static char buff[128];

	memset( buff, 0, sizeof(buff) );
	memset( &initbuf, 0, sizeof(INITBUF) );

	strcpy( initbuf.tuxversion,  _getTuxVersion() );
	strcpy( initbuf.osversion,  _getOSInfo() );
	strcpy( initbuf.tuxbootat,  _getTuxInfo() );
	strcpy( initbuf.agentversion, _getAgentVer() );

	sprintf( buff, "<INITBUF TuxVersion=\"%s\" TuxBootAt=\"%s\" OSVersion=\"%s\" AgentVersion=\"%s\"></INITBUF>",
			initbuf.tuxversion,initbuf.tuxbootat, initbuf.osversion, initbuf.agentversion );
/*
	if( strcmp( initbuf.tuxbootat, SYSTEM_IS_STOP ) == 0 ) {
		return NULL; 
	} else
*/
		return buff;
}

char *
execfun (char opt)
{
    struct tm *rectm = NULL;
    int bufsize = 0;
    char rectime[32];
    char *buff = NULL, *svrbuf = NULL, *quebuf = NULL, *cltbuf = NULL, *sysbuf = NULL, *initbuf = NULL;

	system_flag = 1;

	switch( opt ) {
		case GETINITDATA:
			initbuf = getInitInfo();

			bufsize = strlen(initbuf);

			buff = (char*)malloc( bufsize + 1024 );
			memset (buff, 0, bufsize+1024);

			tm = time (NULL);
			memset( rectime, 0, sizeof(rectime) );
			rectm = localtime (&tm);

			sprintf (rectime, "%d-%02d-%02d %02d:%02d:%02d",
					 rectm->tm_year + 1900, rectm->tm_mon + 1, rectm->tm_mday,
					 rectm->tm_hour, rectm->tm_min, rectm->tm_sec);

			sprintf( buff, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<MONITOR Date=\"%s\">", rectime );

			strcat( buff, initbuf );
			if( system_flag == 0 ) {
				strcat( buff, "<ERROR Reason=\"Can't attached Tuxedo. maybe shutdown.\"></ERROR>");
			}
			strcat( buff, "</MONITOR>" );

			break;
		case GETSVRDATA:
			tm = time (NULL);
			memset( rectime, 0, sizeof(rectime) );
			rectm = localtime (&tm);
			sprintf (rectime, "%d-%02d-%02d %02d:%02d:%02d",
					 rectm->tm_year + 1900, rectm->tm_mon + 1, rectm->tm_mday,
					 rectm->tm_hour, rectm->tm_min, rectm->tm_sec);

			svrbuf = getSrvInfo();
			quebuf = getQueInfo();
			cltbuf = getCltInfo();
			sysbuf = getSysInfo();

			bufsize = strlen(svrbuf) + strlen(quebuf) + strlen(cltbuf) + strlen(sysbuf);

			buff = (char*)malloc( bufsize + 512 );
			memset (buff, 0, bufsize+512);

			sprintf( buff, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<MONITOR Date=\"%s\">", rectime );

			if( system_flag == 0 ) {
				strcat( buff, "<ERROR Reason=\"Can't attached Tuxedo. maybe shutdown.\"></ERROR>");
				strcat( buff, "</MONITOR>" );
				break;
			}
			strcat( buff, svrbuf ); (void)free(svrbuf);
			strcat( buff, quebuf ); (void)free(quebuf);
			strcat( buff, cltbuf ); (void)free(cltbuf);
			strcat( buff, sysbuf ); (void)free(sysbuf);
			strcat( buff, "</MONITOR>" );

			break;
	}

    return ( buff );
}

#ifdef _USEMAIN
int main(int argc, char **argv)
{
	const char usage[] = "Usage: %s -r -s.\n";
	char optstrings[] = "rs";
        int sys = 0, rt = 0, c = 0;

        if ( argc < 2 ) {
                fprintf(stderr, usage, argv[0]);
                exit(0);
        }
        while( (c = getopt(argc, argv, optstrings)) != -1 ){
                switch( c ){
                case 'r':
                        rt = 1;
                        break;
                case 's':
                        sys = 1;
                        break;
                case '?':
                        fprintf(stderr, usage, argv[0]);
                        exit(0);
                }

        }
	tm = time(NULL);
	if ( rt ) {
		getSrvInfo();
		getQueInfo();
		getCltInfo();
	}
	if ( sys ) {
		getSysInfo();
	}

	exit(0);
}
#endif
